package org.hxzon.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExcelUtil {
    private static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class);

    public Workbook read(String filePath) {
        logger.debug(filePath);
        Workbook wb = null;// must reset,or will wrong
        InputStream ins = null;
        try {
            ins = new FileInputStream(filePath);
            wb = WorkbookFactory.create(ins);
            return wb;
        } catch (Exception e) {// InvalidFormatException,IOException,RuntimeException
            throw new RuntimeException(e);
        } finally {
            try {
                ins.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }

        }

    }

    public void write(String absoluteFilePath, Workbook wb) {
        try {
            OutputStream ous = FileUtils.openOutputStream(new File(absoluteFilePath));
            wb.write(ous);
            ous.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void setCellString(Row row, int colIndex, Object value) {
        setCellString(row, colIndex, value.toString());
    }

    public static void setCellString(Row row, int colIndex, String value) {
        setCellString(row, colIndex, value, null);
    }

    public static void setCellString(Row row, int colIndex, String value, CellStyle cellStyle) {
        Cell cell = row.createCell(colIndex);
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(value);
        if (cellStyle != null) {
            cell.setCellStyle(cellStyle);
        }
    }

    public static String getCellString(Sheet sheet, int rowIndex, int colIndex) {
        if (sheet == null) {
            return "";
        }
        return getCellString(sheet.getRow(rowIndex), colIndex);

    }

    public static String getCellString(Row row, int colIndex) {
        if (row == null) {
            return "";
        }
        return getCellString(row.getCell(colIndex));
    }

    public static String getCellString(Cell cell) {
        String value = "";
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:// not escape
                value = cell.getStringCellValue().trim();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
            }
        }
        return value;

    }

    public static boolean isEmptyRow(Row row) {
        for (int i = 0; i < 5; i++) {
            if (!getCellString(row, i).isEmpty()) {
                return false;
            }
        }
        return true;
    }

}
